﻿using DFGK_WMS.IRepository;
using DFGK_WMS.Model.Models;
using DFGK_WMS.WebAPI.Controllers.Result;
using Microsoft.AspNetCore.Mvc;

namespace DFGK_WMS.WebAPI.Controllers.库存管理
{
    /// <summary>
    /// 库存预警
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class TInventoryAlertController : ControllerBase
    {

        private readonly ITInventoryAlertRepository TInventoryAlert;

        public TInventoryAlertController(ITInventoryAlertRepository TInventoryAlert)
        {
            this.TInventoryAlert = TInventoryAlert;
        }

        /// <summary>
        /// 库存预警查询
        /// </summary>
        /// <param name="WarningType"></param>
        /// <param name="warehouse"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetInventoryWarnPagingList(int? WarningType, int? warehouse, int pageIndex, int pageSize)
        {
            if(pageIndex<=0 || pageSize<=0)
            {
                return Ok(BaseResult.Result("400", "分页条件有误！"));
            }
           var list =  TInventoryAlert.GetInventoryWarnPagingList(out var totalCount, WarningType, warehouse, pageIndex, pageSize);
            if (list != null)
            {
                return Ok(BaseResult.Result(list, "查询成功！", "200", totalCount));

            }
            return Ok(BaseResult.Result(null, "查询失败！", "500", 0));
        }

        /// <summary>
        /// 添加预警类型
        /// </summary>
        /// <param name="warningRules"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult AddWarningRules(TWarningRules warningRules)
        {
            string msg;
            var IsOk = TInventoryAlert.AddWarningRules(warningRules, out msg);
            if (IsOk)
            {
                return Ok(BaseResult.Result(IsOk, "添加成功！", "200"));

            }
            return Ok(BaseResult.Result(false, msg, "500" ));
        }

        /// <summary>
        /// 删除预警类型
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete]
        public IActionResult DelWarningRules(int id)
        {
            var IsOk = TInventoryAlert.DelWarningRules(id);
            if (IsOk)
            {
                return Ok(BaseResult.Result(IsOk,"删除成功！", "200"));

            }
            return Ok(BaseResult.Result(false,"删除失败！", "500"));
        }

        /// <summary>
        /// 修改预警类型
        /// </summary>
        /// <param name="warningRules"></param>
        /// <returns></returns>
        [HttpPut]
        public IActionResult UpdWarningRules(TWarningRules warningRules)
        {
            string msg;
            var IsOk = TInventoryAlert.UpdWarningRules(warningRules,out msg);
            if (IsOk)
            {
                return Ok(BaseResult.Result(IsOk,"修改成功！", "200"));

            }
            return Ok(BaseResult.Result(false,msg, "500"));
        }

        /// <summary>
        /// 查询预警类型
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetWarningRules(int id)
        {
            var list = TInventoryAlert.GetWarningRules(id);
            if (list != null)
            {
                return Ok(BaseResult.Result(list,"查询成功！", "200"));

            }
            return Ok(BaseResult.Result(null,"查询失败！", "500"));
        }

        /// <summary>
        /// 查询全部预警类型
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetWarningRulesList(int pageIndex, int pageSize)
        {
            var list = TInventoryAlert.GetWarningRulesList(pageIndex, pageSize);
            if (list != null)
            {
                return Ok(BaseResult.Result(list, "查询成功！", "200"));

            }
            return Ok(BaseResult.Result(null, "查询失败！", "500"));
        }


        /// <summary>
        /// 查询库存预警
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetTInventoryWarnList(int ?WarningType, int pageIndex, int pageSize)
        {
            var list = TInventoryAlert.GetTInventoryWarnList(WarningType,pageIndex, pageSize);
            if (list != null)
            {
                return Ok(BaseResult.Result(list, "查询成功！", "200"));

            }
            return Ok(BaseResult.Result(null, "查询失败！", "500"));
        }

    }
}
